/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 项目名：	rumba
 * 文件名：	LogInfoCmpst.java
 * 模块说明：	
 * 修改历史：
 * Dec 18, 2007 - lxm - 创建。
 */
package com.hd123.rumba.gwt.entry.client;

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.gwtext.client.widgets.MessageBox;
import com.hd123.rumba.gwt.base.client.exception.ClientBizException;

/**
 * 工具条 | 界面部件
 * 
 * @author lxm
 * 
 */
class ToolbarCmpst extends Composite {

  /**
   * 构造函数
   * 
   * @param entryPoint
   *          指定所属模块入口点。
   */
  public ToolbarCmpst(MainFrame entryPoint) {
    super();
    assert entryPoint != null;
    this.entryPoint = entryPoint;
    drawSelf();
  }

  // --------------------------------------------------------------------------
  // 数据
  // --------------------------------------------------------------------------

  private MainFrame entryPoint;

  // --------------------------------------------------------------------------
  // 用户界面
  // --------------------------------------------------------------------------

  private HorizontalPanel pnl;

  /** 首页按钮 */
  private TBButtonCmpst btnHome;
  /** 后退按钮 */
  private TBButtonCmpst btnBackward;
  /** 前进按钮 */
  private TBButtonCmpst btnForward;
  /** 刷新按钮 */
  private TBButtonCmpst btnRefresh;
  /** 打印按钮 */
  private TBButtonCmpst btnPrint;
  /** 修改口令按钮 */
  private TBButtonCmpst btnChangePwd;
  /** 注销登录按钮 */
  private TBButtonCmpst btnLogout;

  private void drawSelf() {
    pnl = new HorizontalPanel();
    pnl.setStyleName("rb-toolbar");
    pnl.add(draw_btnHome());
    pnl.add(drawSeperator());
    pnl.add(draw_btnBackward());
    pnl.add(draw_btnForward());
    pnl.add(draw_btnRefresh());
    pnl.add(draw_btnPrint());
    pnl.add(drawSeperator());
    pnl.add(draw_btnChangePwd());
    pnl.add(drawSeperator());
    pnl.add(draw_btnLogout());
    pnl.add(drawSeperator());
    this.initWidget(pnl);
  }

  private Label drawSeperator() {
    return new Label("") {
      {
        setStyleName("rb-tb-seperator");
      }
    };
  }

  private TBButtonCmpst draw_btnHome() {
    btnHome = new TBButtonCmpst(true, true) {
      public void doClick() {
        doHome();
      }
    };
    btnHome.setCaption("首页");
    btnHome.setIconUrl("images/buttons/home.gif");
    btnHome.setDisabledIconUrl("images/buttons/home-disabled.gif");
    return btnHome;
  }

  private TBButtonCmpst draw_btnBackward() {
    btnBackward = new TBButtonCmpst(true, false) {
      public void doClick() {
        doBackward();
      }
    };
    btnBackward.setCaption("后退");
    btnBackward.setIconUrl("images/buttons/backward.gif");
    btnBackward.setDisabledIconUrl("images/buttons/backward-disabled.gif");
    return btnBackward;
  }

  private TBButtonCmpst draw_btnForward() {
    btnForward = new TBButtonCmpst(false, false) {
      public void doClick() {
        doForward();
      }
    };
    btnForward.setCaption("前进");
    btnForward.setIconUrl("images/buttons/forward.gif");
    btnForward.setDisabledIconUrl("images/buttons/forward-disabled.gif");
    return btnForward;
  }

  private TBButtonCmpst draw_btnRefresh() {
    btnRefresh = new TBButtonCmpst(false, false) {
      public void doClick() {
        doRefresh();
      }
    };
    btnRefresh.setCaption("刷新");
    btnRefresh.setIconUrl("images/buttons/refresh.gif");
    btnRefresh.setDisabledIconUrl("images/buttons/refresh-disabled.gif");
    return btnRefresh;
  }

  private TBButtonCmpst draw_btnPrint() {
    btnPrint = new TBButtonCmpst(false, true) {
      public void doClick() {
        doPrint();
      }
    };
    btnPrint.setCaption("打印");
    btnPrint.setIconUrl("images/buttons/print.gif");
    btnPrint.setDisabledIconUrl("images/buttons/print-disabled.gif");
    return btnPrint;
  }

  private TBButtonCmpst draw_btnChangePwd() {
    btnChangePwd = new TBButtonCmpst(true, true) {
      public void doClick() {
        doChangePwd();
      }
    };
    btnChangePwd.setCaption("修改口令");
    btnChangePwd.setIconUrl("images/buttons/changePwd.gif");
    btnChangePwd.setDisabledIconUrl("images/buttons/changePwd-disabled.gif");
    return btnChangePwd;
  }

  private TBButtonCmpst draw_btnLogout() {
    btnLogout = new TBButtonCmpst(true, true) {
      public void doClick() {
        doLogout();
      }
    };
    btnLogout.setCaption("退出");
    btnLogout.setIconUrl("images/buttons/logout.gif");
    btnLogout.setDisabledIconUrl("images/buttons/logout-disabled.gif");
    return btnLogout;
  }

  private void doHome() {
    try {
      this.entryPoint.showModule(MainFrame.MODULEURL_HOME);
    } catch (ClientBizException e) {
      MessageBox.alert(Window.getTitle(), "无法显示首页页面。\n" + e.getMessage());
    }
  }

  private native void doBackward() /*-{
   var fraId = this.@com.hd123.rumba.gwt.entry.client.ToolbarCmpst::getActionFrameId()();
   if (fraId) $doc.frames(fraId).history.back();
   }-*/;

  private native void doForward() /*-{
   var fraId = this.@com.hd123.rumba.gwt.entry.client.ToolbarCmpst::getActionFrameId()();
   if (fraId) $doc.frames(fraId).history.forward();
   }-*/;

  private native void doRefresh() /*-{
   var fraId = this.@com.hd123.rumba.gwt.entry.client.ToolbarCmpst::getActionFrameId()();
   if (fraId) $doc.frames(fraId).location.reload();
   }-*/;

  private native void doPrint() /*-{
   var fraId = this.@com.hd123.rumba.gwt.entry.client.ToolbarCmpst::getActionFrameId()();
   if (fraId) $doc.frames(fraId).print();
   }-*/;

  private void doChangePwd() {
    try {
      this.entryPoint.showModule(MainFrame.MODULEURL_CHANGEPWD);
    } catch (ClientBizException e) {
      MessageBox.alert(Window.getTitle(), "无法显示修改口令页面。\n" + e.getMessage());
      return;
    }
  }

  private void doLogout() {
    try {
      this.entryPoint.showEntry("_blank");
    } catch (ClientBizException e) {
      MessageBox.alert(Window.getTitle(), "无法显示登录页面。\n" + e.getMessage());
      return;
    }
    closeWindow();
  }

  Element getActionFrameElement() {
    return this.entryPoint.getActiveWindow().getFrame().getElement();
  }

  String getActionFrameId() {
    try {
      return this.entryPoint.getActiveWindow().getFrameId();
    } catch (Exception e) {
      return null;
    }
  }

  private native void closeWindow() /*-{
   top.close();
   }-*/;
}
